﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :NewsClass_E.aspx.cs
//        description :新闻类别信息编辑页面
//
//        created by 邹兴武 at  2010-12-29
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace huaguwang.hgmanage.News
{
    public partial class NewsClass_E : System.Web.UI.Page
    {
        private readonly D_T_NewsCate_Exp _dal = new D_T_NewsCate_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserToolBar1.EditInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              SetBut,
             UserConst.Exp_ToolBarErr
           );
            if (!IsPostBack)
            {
                this.HGDownListTree1.Attributes.Add(
                    "onchange",
                    "javascript:$(\"#" + this.txtName.ClientID + "\").val(\"\")"
                    );
                //初始化下拉树控件
                this.HGDownListTree1.Exp_BindNewsClassTree();
                if (Request.Params["id"] != null)
                {
                    InitData(Request.Params["id"].Exp_IntTryParse());
                    ViewState.Add("id", Request.Params["id"]);
                    this.HGDownListTree1.Enabled = false;
                }
            }
        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static bool Exists(string title, int petid, int id)
        {
            D_T_NewsCate_Exp _dal = new D_T_NewsCate_Exp();
            return _dal.ExistsExp(title, petid, id);
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
                {
                    var model = CreateModel();
                    if (model == null)
                    {
                        return;
                    }
                    else
                    {
                        if (IsEffective(model.ParentId))
                        {
                            this.Exp_PageAlter("当前类别已经存在内容信息,不能为其添加下级类别!");
                            return;
                        }
                    }
                    var iRet = 0;
                    Log_Type le = Log_Type.add;
                    if (model.id > 0)
                    {
                        var tempModel = _dal.GetModel(model.id);
                        model.OrderBy = tempModel.OrderBy;
                        if (tempModel == null || !tempModel.Flag)
                        {
                            this.Exp_PageAlter("当前信息不存在,可能已经被其他人删除!");
                            return;
                        }
                        iRet = _dal.UpdateExp(model);
                        le = Log_Type.update;
                    }
                    else
                    {
                        model.OrderBy = _dal.GetMaxOrder(model.ParentId) + 1;//自动加1
                        iRet = _dal.AddExp(model);
                    }

                    if (iRet > 0)
                    {
                        //添加成功后要修改其父结点的ChildPath
                        if (model.id <= 0)
                        {
                            var tModel = _dal.GetModel(iRet);
                            var idList = tModel.ParentPath.Exp_Trim();
                            _dal.UpdateExp(idList);
                        }

                        //写日志
                        this.Exp_PageWriteLog(le, "新闻类别", iRet);
                        var key = ViewState["id"] == null ? "" : "?selid=" + ViewState["id"];
                        this.Exp_PageAlterHref("操作成功", "newsclass.aspx"+key);
                    }
                    else
                    {
                        this.Exp_PageAlter("操作失败");
                    }
                });
        }

        /// <summary>
        /// 返回
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var key = ViewState["id"] == null ? "" : "?selid="+ViewState["id"];
            Response.Redirect("newsclass.aspx"+key);
        }

        private void InitData(int id)
        {
            var model = _dal.GetModel(id);

            this.txtName.Text = model.CateName;
            this.txtName.ToolTip = model.CateName;
            this.HGDownListTree1.Text = model.ParentId.Exp_Trim();
        }

        private M_T_NewsCate CreateModel()
        {
            var model = new M_T_NewsCate();
            model.ParentId = this.HGDownListTree1.Text.Exp_IntTryParse();
            if (model.ParentId <= 0)
            {
                model.Depth = 1;
                model.ParentId = 0;
            }
            else
            {
                var pModel = _dal.GetModel(model.ParentId);
                if (pModel == null || pModel.Flag == false)
                {
                    this.Exp_PageAlter("父类别不存在");
                    return null;
                }
                model.Depth = pModel.Depth + 1;
                model.ParentId = pModel.id;
            }

            model.id = ViewState["id"].Exp_IntTryParse();
            model.CateName = this.txtName.Text.Exp_Trim();
            model.ParentPath = "0";
            model.ChildPath = string.Empty;
            model.Explain = string.Empty;
            model.CateContent = string.Empty;
            model.Flag = true;
            model.DescriptionWord = string.Empty;
            model.KeyWords = string.Empty;
            model.QueryKey = string.Empty;
            model.OrderPath = "0";

            return model;
        }

        private bool IsEffective(int parentid)
        {
            if (this._dal.IsEffective(parentid))
            {
                this.Exp_PageAlter("该类别已有内容信息,不能为其添加下级类别");
                return true;
            }
            return false;
        }
    }
}
